package com.qx.chat.system.dao;

import java.util.List;
import java.util.regex.Pattern;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.core.query.Update;
import org.springframework.stereotype.Component;

import com.qx.chat.system.entity.DeptUser;

/**
 * TODO
 * @author cyf
 * @version TODO
 */

@Component
public class DeptUserDaoImpl implements AbstractRepository<DeptUser> {

	@Autowired
	private MongoTemplate mongoTemplate;   

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findAll()
	 */
	@Override
	public List<DeptUser> findAll() {
		// TODO Auto-generated method stub
		return getMongoTemplate().find(new Query(), DeptUser.class);   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findAndModify(java.lang.String)
	 */
	@Override
	public void findAndModify(String id,Update update) {
		// TODO Auto-generated method stub
		//new Query(Criteria.where("id").is(id)), new Update().inc("age", 3)
		
		getMongoTemplate().updateFirst(new Query(Criteria.where("id").is(id)), update,DeptUser.class);

	}

	@Override
	public void findAndModify(String key,String value,Update update) {
		// TODO Auto-generated method stub
		//new Query(Criteria.where("id").is(id)), new Update().inc("age", 3)
		
		getMongoTemplate().updateFirst(new Query(Criteria.where(key).is(value)), update,DeptUser.class);

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findByRegex(java.lang.String)
	 */
	@Override
	public List<DeptUser> findByRegex(String regex) {
		// TODO Auto-generated method stub
		 Pattern pattern = Pattern.compile(regex,Pattern.CASE_INSENSITIVE);   
	      Criteria criteria = new Criteria("name").regex(pattern.toString());   
	        return getMongoTemplate().find(new Query(criteria), DeptUser.class);   

	}
	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findById(java.lang.String)
	 */
	@Override
	public DeptUser findById(String id) {
		// TODO Auto-generated method stub
		 return getMongoTemplate().findById(id, DeptUser.class);   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findOne(java.lang.String)
	 */
	@Override
	public DeptUser findOne(String key,String value) {
		// TODO Auto-generated method stub
		 return getMongoTemplate().findOne(new Query(Criteria.where(key).is(value)), DeptUser.class);   

	}

	@Override
	public DeptUser findOne(String[] key, String[] value) {
		// TODO Auto-generated method stub
		Query query = new Query();
		for (int i = 0; i < key.length; i++) {
			query.addCriteria(Criteria.where(key[i]).is(value[i]));
		}
		return getMongoTemplate().findOne(query, DeptUser.class); 
	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#findOne(java.lang.String)
	 */
	@Override
	public List<DeptUser> findList(String key,String value) {
		// TODO Auto-generated method stub
		 return getMongoTemplate().find(new Query(Criteria.where(key).is(value)), DeptUser.class);   

	}

	@Override
	public List<DeptUser> findList(String[] key, String[] value) {
		// TODO Auto-generated method stub
		Query query = new Query();
		for (int i = 0; i < key.length; i++) {
			query.addCriteria(Criteria.where(key[i]).is(value[i]));
		}
		return getMongoTemplate().find(query, DeptUser.class); 
	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#insert(com.mongo.bean.DeptUser)
	 */
	@Override
	public void insert(DeptUser entity) {
		// TODO Auto-generated method stub
		getMongoTemplate().insert(entity);   
	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#removeAll()
	 */
	@Override
	public void removeAll() {
		// TODO Auto-generated method stub
		List<DeptUser> list = this.findAll();   
        if(list != null){   
            for(DeptUser entity : list){   
                getMongoTemplate().remove(entity);   
            }   
        }   

	}

	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#removeOne(java.lang.String)
	 */
	@Override
	public void removeById(String id) {
		Criteria criteria = Criteria.where("id").in(id);   
		Query query = new Query(criteria);   
        getMongoTemplate().remove(query, DeptUser.class);
	}
	
	/* (non-Javadoc)
	 * @see com.mongo.dao.AbstractRepository#removeOne(java.lang.String)
	 */
	@Override
	public void removeByKey(String key,String value) {
		Criteria criteria = Criteria.where(key).in(value);   
		Query query = new Query(criteria);   
        getMongoTemplate().remove(query, DeptUser.class);
	}

    /**
	 * 
	 * @Title: findListByPage @Description: 根据查询条件直接查询 @param c @param
	 * orders @param pageIndex @param pageSize @return List<E> @throws
	 */
	public List<DeptUser> findListByPage(Criteria c, List<Order> orders, int pageIndex, int pageSize) {
		Query query = new Query(c);
		Sort sort = null;
		if (orders != null && orders.size() > 0) {
			sort = new Sort(orders);
		}
		if (sort != null) {
			query = query.with(sort);
		}
		if (pageSize > 0) {
			query.skip((pageIndex - 1) * pageSize);
			query.limit(pageSize);
		}
		return mongoTemplate.find(query, DeptUser.class);
	}

    /**
	 * 
	 * @Title: findListByPage @Description: 根据查询条件直接查询 @param c @param
	 * orders @param pageIndex @param pageSize @return List<E> @throws
	 */
	public List<DeptUser> findList(Criteria c, List<Order> orders) {
		Query query = new Query(c);
		Sort sort = null;
		if (orders != null && orders.size() > 0) {
			sort = new Sort(orders);
		}
		if (sort != null) {
			query = query.with(sort);
		}
		return mongoTemplate.find(query, DeptUser.class);
	}

	/**
	 * @return the mongoTemplate
	 */
	public MongoTemplate getMongoTemplate() {
		return mongoTemplate;
	}

	/**
	 * @param mongoTemplate the mongoTemplate to set
	 */
	public void setMongoTemplate(MongoTemplate mongoTemplate) {
		this.mongoTemplate = mongoTemplate;
	}

}
